#include "globalParameter.h"

#include <QtNetwork>

#include "clientsocket.h"
#include <QThread>
#include <windows.h>


#include "protocol/Command.h"
#include "protocol/baseptc.h"
#include "protocol/gps.h"
#include "protocol/srvloginansToCar.h"
#include "protocol/carlogin.h"
#include "protocol/srvCarRequestPassengersAnsToCar.h"
#include "protocol/CarRequestPassenger.h"
#include "protocol/srvCarRequestPassengersAnsToCar.h"
#include "protocol/srvordersuccess.h"

#include "QTextCodec"

ClientSocket::ClientSocket(QObject *parent)
    : QTcpSocket(parent)
{
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GBK"));

    connect(this, SIGNAL(readyRead()), this, SLOT(readClient()));
    connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater()));

    nextBlockSize = 0;
}

void ClientSocket::readClient()
{
    QDataStream in(this);
    in.setVersion(QDataStream::Qt_4_3);

    qint32 source, command, size;

    if (nextBlockSize == 0) {
        if (bytesAvailable() < sizeof(protocal::BasePtc))
            return;

        in >> source >> command >> size;

        nextBlockSize = size - sizeof(protocal::BasePtc);
    }

    if (bytesAvailable() < nextBlockSize)
        return;

    QDataStream out(this);
    if(command == protocal::CAR_LOGIN)
    {
        QString username;
        QString password;

        in >> username >> password;


        source = protocal::SERVER;
        command = protocal::SRV_LOGINANS;
        size = sizeof(protocal::SrvLoginAns);
        out << source << command << size;

        out << (short)1 << "123456";


        nextBlockSize = 0;
        return;
    }

//    if (command == protocal::CLI_BOOK)
//    {
//        QString userID;
//        in >> userID;

//        float lat, lon;
//        int time;
//        // start
//        in >> lat >> lon >> time;
//        // end
//        in >> lat >> lon >> time;

//        // wait tiem
//        int waitingTime;
//        in >> waitingTime;//minutes

//            source = protocal::SERVER;
//            command = protocal::SRV_DRIVER_ANS;
//            size = sizeof(protocal::SrvDriverAns);
//            out << source << command << size;
//        /*
//        char car_id[10];
//        char licence_plate[10];
//        char driver_name[10];
//        char company[10];
//        short honesty_degree;
//        short arri_time;
//        GPS gps;*/
//            out << QString("98765") << QString("abcdefg")
//                << QString("sadan") << QString("hell")
//                << 2 << 3
//                << 10.4 << 32.4 << 10;

//    }

    if (command == protocal::CAR_REQUEST_PASSENGERS)
    {
        // oder success
        QString carID;
        in >> carID;

            source = protocal::SERVER;
            command = protocal::SRV_CAR_REQUEST_PASSENGERS;
            size = sizeof(protocal::SrvCarQequestPassengersAnsToCar);
            out << source << command << size;

            /*
            char client_id[10];
            char client_name[10];
            int arrivingTime; //min
            protocal::GPS startPos;
            protocal::GPS endPos;
            char mobile_phone[12];
            */

            out << "123321" << "nick" << 12\
                << (quint32)30 << (quint32)123\
                << (quint32)456 << (quint32)789\
                << "99988877745";


            nextBlockSize = 0;
            return;
    }

    if (command == protocal::CAR_CHOOSE_CLIENT)
    {
        // oder success
        QString carID;
        char *temp = new char [10];
        in >> temp;
        carID = QString(temp);

        QString userID;
        in >> temp;
        userID = QString(temp);
        delete [] temp;

            source = protocal::SERVER;
            command = protocal::SRV_ORDER_SUCCESS;
            size = sizeof(protocal::SrvOrderSuccess);
            out << source << command << size;

            /*
            char client_id[10];
            char client_name[10];
            int arrivingTime; //min
            protocal::GPS startPos;
            protocal::GPS endPos;
            char mobile_phone[12];
            */

            out << (qint32)1 << "123321" << "nick" << "99988877745";

            nextBlockSize = 0;
            return;
    }

//    if(command == protocal::CLI_ABANDON_BOOK)
//    {
//        //qDebug << QString("client has abandoned the book\n");
//    }

//    if(command == protocal::CLI_GET_ON_CAR)
//    {
//        source = protocal::SERVER;
//        command = protocal::SRV_GET_CLIENT_ON_MESSAGE;
//        size = sizeof(protocal::SrvGetOnCarAns);
//        out << source << command << size;
//    }

//    if(command == protocal::CLI_GET_OFF_CAR)
//    {
//        source = protocal::SERVER;
//        command = protocal::SRV_GET_CLIENT_OFF_MESSAGE;
//        size = sizeof(protocal::SrvGetOffCarAns);
//        out << source << command << size;
//    }
}
